
#include "SEGGER_RTT.h"
#include "stdint.h"
#include "debug.h"
#include "qdec/qdec.h"


void delay_ms(uint16_t cnt) {
	for(uint16_t i = 0; i<cnt;i++) {
		uint16_t d = 10000;
		while(d--);
	}
}

#include "mcu.h"
#include "same51n20a.h"


uint8_t command_buf[128];
void uart_rx_block_test() {
	if(0u != uart_receive_block(command_buf, 20)) {
		uart_send_str("timeout\r\n");
	}
	else {
		uart_send_str("get:");
		uart_send_str((const char*)command_buf);
	}
}

void uart_rx_interrupt_test() {
	if(_uart0RxCount >= 20) {
		uart_send_str("get:");
		uart_send_str((const char*)_uart0RxBuffer);
		_uart0RxCount = 0;
	}
}

void uart_rx_frame_test() {
	if(frameFlag) {
		uart_send_str("frame:");
		uart_send_bytes((const uint8_t*)_uart0RxBuffer, _uart0RxCount);
		uart_send_str("\r\n");
		frameFlag = 0;
		_uart0RxCount = 0;
	}
}

int main(void) {
	SEGGER_RTT_Init();
  	SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
	mcu_init();
	delay_ms(100);
	uint32_t board_rev = PORT_REGS->GROUP[0].PORT_IN & (PORT_PA06 | PORT_PA07);
	SEGGER_RTT_printf(0, "board_rev: %x\n", board_rev);
	for(;;) {
		delay_ms(50);
		sint16 count = qdec_get_count();
		LOG_NO_LABEL(INFO, "PERIPHERAL=%d\r\n", count);
    }
	return 0;
}